<?php

class ServiceMonitorClient
{
	const ServerUrlBase = "http://localhost:18365/Status/Report/";
	
	private
		$username,
		$password,
		$serviceId,
		$curl,
		$ouputEnabled = true;
	
	public function __construct($username, $password, $serviceId)
	{
		$this->username = $username;
		$this->password = $password;
		$this->serviceId = $serviceId;
		
		if (!is_callable("curl_init"))
		{
			if (!dl("php_curl.dll"))
			{
				throw new Exception("curl is not found");;
			}
		}
		
		$this->out("starting");
		
		$this->curl = curl_init();
		curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($this->curl, CURLOPT_USERPWD, "{$this->username}:{$this->password}");
	}
	
	public function __destruct()
	{
		curl_close($this->curl);
	}
	
	private function out($string = "")
	{
		if ($this->ouputEnabled)
		{
			echo $string."\n<br />";
		}
	}

	public function sendStatus($statusString)
	{
		$this->out("sending status...");
		
		curl_setopt($this->curl, CURLOPT_URL, self::ServerUrlBase."?id=".$this->serviceId);
		curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Content-Type' => 'text/plain'));
		curl_setopt($this->curl, CURLOPT_POST, true);
		curl_setopt($this->curl, CURLOPT_POSTFIELDS, $statusString);
		$result = curl_exec($this->curl);
		
		$this->out("status was send");
	}
	
	public function disableOutput()
	{
		$this->outputEnabled = false;
	}
	
	public function enableOutput()
	{
		$this->outputEnabled = true;
	}
}

?>